<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件下载示例</title>
  <style>
    /* 页面整体样式 */
    body {
      font-family: 'Arial', sans-serif;
      background-color: #f4f4f9;
      color: #333;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }

    /* 下载按钮样式 */
    .download-button {
      padding: 15px 30px;
      background-color: #4CAF50;
      color: white;
      font-size: 16px;
      font-weight: bold;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    .download-button:hover {
      background-color: #45a049;
    }

    /* 信息显示区域 */
    .info-box {
      margin-top: 20px;
      padding: 15px;
      background-color: #fff;
      border: 1px solid #ddd;
      border-radius: 5px;
      text-align: center;
      font-size: 18px;
      width: 300px;
      display: none;
    }

    .success {
      color: #4CAF50;
    }

    .downloading {
      color: #ffa500;
    }

    /* 页面加载时的动画 */
    .loading {
      color: #007bff;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div>
    <button class="download-button" onclick="startDownload()">开始下载</button>
    <div id="infoBox" class="info-box"></div>
  </div>

  <script>
    // 下载函数
    const downloadIframe = (path, resolve, time = 1000) => {
      let iframe = document.createElement('iframe');
      iframe.src = path;
      iframe.style.display = 'none';
      document.body.appendChild(iframe);
      let timer = null;
      let handler = function () {
        const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
        if (['complete', 'interactive'].indexOf(iframeDoc.readyState) > -1) {
          document.body.removeChild(iframe);
          if (timer) clearTimeout(timer);
          resolve('success');
        } else {
          timer = setTimeout(handler, time);
          resolve('downloading');
        }
      };
      handler();
    };

    // 处理下载按钮点击事件
    function startDownload() {
      const infoBox = document.getElementById('infoBox');
      infoBox.style.display = 'block';
      infoBox.textContent = '文件正在下载...';
      infoBox.classList.remove('success', 'downloading');
      infoBox.classList.add('loading');

      downloadIframe('./1.png', (status) => {
        if (status === 'downloading') {
          infoBox.textContent = '文件正在下载...';
          infoBox.classList.remove('success');
          infoBox.classList.add('downloading');
        } else if (status === 'success') {
          infoBox.textContent = '下载成功！';
          infoBox.classList.remove('downloading');
          infoBox.classList.add('success');
        }
      });
    }
  </script>
</body>
</html>
